package cn.maxxieyi.aichatbot.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.http.ContentType;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import cn.maxxieyi.aichatbot.dto.AiChatRequest;
import cn.maxxieyi.aichatbot.dto.AiChatResponse;
import cn.maxxieyi.aichatbot.dto.GmoreAiChatRequest;
import cn.maxxieyi.aichatbot.dto.GmoreAiChatResponse;
import cn.maxxieyi.aichatbot.exception.CommonException;
import cn.maxxieyi.aichatbot.service.AiApiService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class GmoreAiApiServiceImpl implements AiApiService {

    @Value("${ai.gmore-ai.apiUrl}")
    private String apiUrl = "https://gmoreai.zhidianjh.com:8443/openapi/bot/chat";

    /**
     * 文本对话接口
     *
     * @param aiChatRequest
     * @return
     */
    @Override
    public AiChatResponse chatText(AiChatRequest aiChatRequest) {
        GmoreAiChatRequest gmoreAiChatRequest = (GmoreAiChatRequest)aiChatRequest;
        log.info("GmoreAI request:"+JSON.toJSONString(gmoreAiChatRequest));
        String result = HttpRequest.post(apiUrl)
                .header(Header.CONTENT_TYPE.getValue(), ContentType.JSON.getValue())
                .header(Header.AUTHORIZATION.getValue(), gmoreAiChatRequest.getApiKey())
                .body(JSON.toJSONString(gmoreAiChatRequest))
//                .timeout(10000)
                .execute().body();

        log.info("GmoreAI response:"+result);
        if(!JSONUtil.isTypeJSONArray(result)){
            log.error("GmoreAiApiServiceImpl.chatText error:{}", result);
            throw new CommonException(result);
        }

        List<GmoreAiChatResponse> aiChatResponseList = JSON.parseArray(result, GmoreAiChatResponse.class);
        AiChatResponse aiChatResponse = null;
        if(CollUtil.isNotEmpty(aiChatResponseList)){
            aiChatResponse = aiChatResponseList.get(aiChatResponseList.size()-1);
            aiChatResponse.setSuccess(true);
            aiChatResponse.setReplyContent(aiChatResponseList.stream().filter(res -> "answer".equals(res.getType()))
                    .map(GmoreAiChatResponse::getContent).collect(Collectors.joining()));
        }else{
            aiChatResponse.setSuccess(false);
        }

        return aiChatResponse;
    }

    /**
     * @param receiveMsg
     * @param wxBotAccount
     * @return
     */
    @Override
    public GmoreAiChatRequest buildChatRequest(JSONObject params) {

        GmoreAiChatRequest aiChatRequest = GmoreAiChatRequest.builder()
                .apiKey(params.getString("aiApiKey"))
                .reqId(IdUtil.objectId())
                .message(params.getString("content"))
                .simpleOut(true)
                .build();

        return aiChatRequest;
    }
}
